package com.ah.hf.service.impl;

import com.ah.hf.common.CustomException;
import com.ah.hf.dto.SetmealDto;
import com.ah.hf.entity.Setmeal;
import com.ah.hf.entity.SetmealDish;
import com.ah.hf.mapper.SetmealMapper;
import com.ah.hf.service.SetmealDishService;
import com.ah.hf.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.javassist.bytecode.stackmap.TypeData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper , Setmeal>
        implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    //开启注解
    @Transactional
    //双表添加
    public void saveWithDish(SetmealDto setmealDto) {
        //调用save方法进行 insert into
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //不等于空，进行保存
        if (setmealDishes != null){

            //可以使用stream流进行添加 SetmealId 值
            /*setmealDishes  = setmealDishes.stream().map((item)->{
               item.setSetmealId(setmealDto.getId());
               return item;
            }).collect(Collectors.toList());
            setmealDishService.saveBatch(setmealDishes);*/

            for (SetmealDish setmealDish : setmealDishes){
                setmealDish.setSetmealId(setmealDto.getId());
                //insert into
                setmealDishService.save(setmealDish);
            }
        }
    }
    @Override
    //开启事务
    @Transactional
    //删除操作
    public void deleteWithDish(List<Long> ids){
        LambdaQueryWrapper<Setmeal> qw1 =new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SetmealDish> qw =new LambdaQueryWrapper<>();
        //id条件
        qw1.in(Setmeal::getId,ids);
        //并且status等于1
        qw1.eq(Setmeal::getStatus,1);
        int count = this.count(qw1);
        if (count >0){
            //抛处异常
            throw new CustomException("当前状态位销售状态，不可删除");
        }
        //删除id
        this.removeByIds(ids);
        //where id in(? , ?)
        qw.in(SetmealDish::getSetmealId,ids);
        //删除
        setmealDishService.remove(qw);

        //查询是否停售
        /*for (Long id : ids){ // 1 2
            Setmeal setmeal = this.getById(id);
            if (setmeal.getStatus() == 1){
                throw new CustomException("当前状态位销售状态，不可删除");
            }
        }

        //删除套餐中的信息
        this.removeByIds(ids);

        //SetmealId 等于 Setmeal.id
        for (Long id : ids){
            //条件构造器
            LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
            qw.eq(SetmealDish::getSetmealId,id);
            setmealDishService.remove(qw);
        }*/
    }
}
